<template>
  <div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :scroll="{ x: true }"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      class="j-table-force-nowrap"
      @change="handleTableChange"
    >
      <template slot="payCondition" slot-scope="text">
        <j-ellipsis :value="text" :length="15" />
      </template>
      <template slot="imgSlot" slot-scope="text">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
        <img
          v-else
          :src="getImgView(text)"
          height="25px"
          alt=""
          style="max-width: 80px; font-size: 12px; font-style: italic"
        />
      </template>
      <template slot="fileSlot" slot-scope="text">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
          下载
        </a-button>
      </template>

      <div slot="action" slot-scope="text, record" v-if="record.contractBpmStatus === '3'">
        <div v-has="'ContractPurchaseApplyPayment:edit'">
          <a v-if="record.bpmStatus === '1'" @click="paymentApplyHandleDetail(record)">申请</a>
          <template v-if="record.auditDataId">
            <a-divider type="vertical" v-if="record.auditDataId && record.bpmStatus === '1'" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多<a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="paymentApplyHandleDetail(record, true)">详情</a>
                </a-menu-item>
                <a-menu-item v-if="record.bpmStatus === '1'">
                  <a @click="startApplyInvoiceProcess(record)">发起流程</a>
                </a-menu-item>
                <a-menu-item v-if="record.bpmStatus !== '1'" @click="handleTrack(record)">审批进度</a-menu-item>
              </a-menu>
            </a-dropdown>
          </template>
        </div>
        <!-- 财务，付款确认 -->
        <span v-has="'ContractPurchase:sure'">
          <a v-if="record.auditDataId" @click="paymentApplyHandleDetail(record, true)">详情</a>
          <a-divider type="vertical" v-if="record.auditDataId" />
          <a @click="handleEdit(record)">确认</a>
        </span>
      </div>
    </a-table>
    <contract-purchase-payment-modal ref="modalForm" @ok="modalFormOk"></contract-purchase-payment-modal>
    <contract-purchase-apply-payment-modal
      ref="paymentApplyModalForm"
      @ok="paymentApplyOk"
    ></contract-purchase-apply-payment-modal>
    <bpm-process-track-modal ref="bpmProcessTrackModal"></bpm-process-track-modal>
    <bpm-biz-task-deal-modal
      ref="taskDealModal"
      :path="path"
      :formData="formData"
      @ok="handleOk"
    ></bpm-biz-task-deal-modal>
  </div>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgBpmListMixin } from '@/mixins/JeecgBpmListMixin'
import { getAction, postAction } from '@api/manage'
import ContractPurchasePaymentModal from './modules/ContractPurchasePaymentModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import ContractPurchaseApplyPaymentModal from '@views/hifar/contract/modules/ContractPurchaseApplyPaymentModal'
import BpmBizTaskDealModal from '@views/modules/bpmbiz/common/BpmBizTaskDealModal'
import BpmProcessTrackModal from '@views/modules/bpmbiz/common/BpmProcessTrackModal'
import { toFixedTwo } from '@/utils/util'

export default {
  name: 'ContractPurchasePaymentList',
  mixins: [JeecgBpmListMixin, mixinDevice],
  components: {
    ContractPurchasePaymentModal,
    ContractPurchaseApplyPaymentModal,
    BpmBizTaskDealModal,
    BpmProcessTrackModal,
  },
  props: {
    mainId: {
      type: String,
      default: '',
      required: false,
    },
  },
  watch: {
    mainId: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          // this.clearList()
        } else {
          this.queryParam['contractId'] = val
          this.loadData(1)
        }
      },
    },
  },
  data() {
    return {
      description: '采购合同-付款管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '付款条件',
          align: 'center',
          dataIndex: 'payCondition',
          scopedSlots: { customRender: 'payCondition' },
        },
        {
          title: '计划付款金额（元）',
          align: 'center',
          dataIndex: 'expectMoney',
        },
        {
          title: '计划付款时间',
          align: 'center',
          dataIndex: 'expectTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '实际付款金额（元）',
          align: 'center',
          dataIndex: 'actualMoney',
        },
        {
          title: '实际付款时间',
          align: 'center',
          dataIndex: 'actualTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '付款完成',
          align: 'center',
          dataIndex: 'paymentCompleted_dictText',
          // customRender: (text) => (text ? filterMultiDictText(this.dictOptions['billing'], text) : ''),
        },
        // {
        //   title: '开票比例',
        //   align: 'center',
        //   dataIndex: 'billingRatio',
        // },
        // {
        //   title: '创建者',
        //   align: 'center',
        //   dataIndex: 'createBy',
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/contract/contractPurchasePayment/list',
        delete: '/contract/contractPurchasePayment/delete',
        deleteBatch: '/contract/contractPurchasePayment/deleteBatch',
        exportXlsUrl: '/contract/contractPurchasePayment/exportXls',
        importExcelUrl: 'contract/contractPurchasePayment/importExcel',
        findByPaymentId: '/contract/contractPurchaseApplyPayment/findByPaymentId',
      },
      flowCode: 'dev_hf_contract_purchase_apply_payment_001',
      dictOptions: {},
      superFieldList: [],
      queryModel: true,
    }
  },
  created() {},
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.dataSource.forEach((v) => {
            v.expectMoney = toFixedTwo(v.expectMoney)
            v.actualMoney = toFixedTwo(v.actualMoney)
          })
          this.ipagination.total = res.result.total
        }
        this.loading = false
      })
    },
    handleEdit: function (record) {
      record.type = 'sure'
      this.$refs.modalForm.edit(record);
      this.$refs.modalForm.title = "确认";
      this.$refs.modalForm.disableSubmit = false;
    },
    startApplyInvoiceProcess(record) {
      this.$confirm({
        title: '提示',
        content: '确认提交流程吗?',
        onOk: () => {
          let params = {
            flowCode: this.flowCode,
            id: record.auditDataId,
            formUrl: 'hifar/contract/modules/ContractPurchaseApplyPaymentForm',
            formUrlMobile: '',
          }
          postAction(this.url.startProcess, params)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.loadData()
                this.onClearSelected()
              } else {
                this.$message.warning(res.message)
              }
            })
            .catch((e) => {
              this.$message.warning('不识别的请求!')
            })
        },
      })
    },
    handleTrack(record) {
      var params = { flowCode: this.flowCode, dataId: record.auditDataId } //查询条件
      this.$refs.bpmProcessTrackModal.handleTrack(params)
      this.$refs.bpmProcessTrackModal.title = '审批跟踪记录'
    },
    paymentApplyHandleDetail(record, disableSubmit) {
      let saleApplyInvoice = {}
      let that = this
      getAction(this.url.findByPaymentId, { id: record.id }).then((res) => {
        if (res.success) {
          saleApplyInvoice = res.result
          that.$refs.paymentApplyModalForm.edit(saleApplyInvoice)
          that.$refs.paymentApplyModalForm.title = '付款申请'
          that.$refs.paymentApplyModalForm.disableSubmit = disableSubmit
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    paymentApplyOk() {
      this.loadData(1)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>